package com.team.appinfo.bgcontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Controller
public class DownLoadController {
    @RequestMapping(value = "/downLoad")
    public  void downLoad(HttpServletRequest request, HttpServletResponse response, String path, String fname) throws IOException {

        String realPath = request.getServletContext().getRealPath(path);
        FileInputStream fis = new FileInputStream(realPath);
        byte by[] = new byte[fis.available()];
        fis.read(by);
        fis.close();

        response.setHeader("Content-Disposition","attachment;fileName="+fname);
        response.setContentType("image/jpeg");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(by);
        outputStream.flush();
        outputStream.close();
    }
}
